import { _decorator, Button, Label, UITransform } from 'cc';
import { BasePopUp } from '../../../frame/component/ui.pop-up';
import { ClientAPI } from '../../api/client.api';
import GameDC from '../../manager/data.game.center';
import GameAssets from '../../manager/game.assets';
import GameManager from '../../manager/game.manager';
import { Float } from '../page/float';
import type { Popup } from '../page/popup';
const { ccclass, property } = _decorator;

@ccclass('Resurgence')
export class Resurgence extends BasePopUp<typeof Popup, boolean> {
	protected default_return: null;

	private ui_container: UITransform;

	protected initAfterOnLoad(){
		this.ui_container = this.node.getChildByName('container').getComponent(UITransform);
		this.renderLocalUI();
	}

	protected _click_event_: { [name: string]: (this: Resurgence, button: Button) => void; } = {
		close(btn){
			btn.interactable = false;
			this.exit(false).then(()=>btn.interactable = true);
		},
		async diamond(btn){
			btn.interactable = false;

			let diamond = GameDC.User.diamond;

			if(diamond >= 100){
				GameManager.changeDiamond(-100);
				GameDC.Trigger.CHANGE_DIAMOND = -100;
				await this.exit(true);
			}else{
				Float.Hint(GameAssets.getLangText(22));
			}

			btn.interactable = true;
		},
	 	async video(btn){
			btn.interactable = false;
			ClientAPI.AD('incentive');
			await this.exit(true);
			btn.interactable = true;
		}
	}

	private async exit(confirm: boolean){
		await BasePopUp.jelly_leave(this.ui_container.node);
		this.close(confirm);
		GameDC.Trigger.HILIT_DIAMOND = false;
	}

	public onShow(){
		GameDC.Trigger.HILIT_DIAMOND = true;
		BasePopUp.jelly_enter(this.ui_container.node);
	}

	private renderLocalUI(){
		let lbl_map = this.ui_container.node.getComponentsInChildren(Label).reduce((map, el)=>{
			map.set(el.node.name, el);
			return map;
		}, new Map<string, Label>());

		(<const>[
			['lbl-head', 32],
			['lbl-diamond', 33],
			['lbl-video', 34],
			['close', 35]
		]).forEach(([name, idx])=>{
			lbl_map.get(name).string = GameAssets.getLangText(idx);
		});
	}
}

